package com.gpframework.module.controller.common;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.common.domain.GpCommonStartPage;
import com.gpframework.module.common.domain.req.GpCommonStartPageAdd;
import com.gpframework.module.common.domain.req.GpCommonStartPageReq;
import com.gpframework.module.common.domain.req.GpCommonStartPageUpdate;
import com.gpframework.module.common.domain.vo.GpCommonStartPageVo;
import com.gpframework.module.common.service.IGpCommonStartPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpCommonStartPageController
 * @Author gpframework
 * @Date 2021-12-09
 * @Version 1.0.0
 * @Description 启动页Controller
 */
@Validated
@Api(tags = "启动页")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/common/gpCommonStartPage")
public class GpCommonStartPageController extends BaseController {

    private final IGpCommonStartPageService iGpCommonStartPageService;

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonStartPage
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('common:gpCommonStartPage:list')")
    @GetMapping("/list")
    public TableDataInfo<GpCommonStartPageVo> list(GpCommonStartPageReq gpCommonStartPageReq) {
        startPage();
        QueryWrapper<GpCommonStartPage> lqw = new QueryWrapper<GpCommonStartPage>();
        lqw.orderByDesc("gp.update_time");
        gpCommonStartPageReq.generatorQuery(lqw, true);
        List<GpCommonStartPageVo> list = iGpCommonStartPageService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonStartPage
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('common:gpCommonStartPage:add')")
    @Log(title = "添加启动页", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpCommonStartPageAdd gpCommonStartPageAdd) {
        GpCommonStartPage gpCommonStartPage = Convert.convert(new TypeReference<GpCommonStartPage>() {
        }, gpCommonStartPageAdd);
        return toAjax(iGpCommonStartPageService.save(gpCommonStartPage) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonStartPage
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('common:gpCommonStartPage:edit')")
    @Log(title = "修改启动页", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCommonStartPageUpdate gpCommonStartPageUpdate) {
        GpCommonStartPage gpCommonStartPage = Convert.convert(new TypeReference<GpCommonStartPage>() {
        }, gpCommonStartPageUpdate);
        return toAjax(iGpCommonStartPageService.updateById(gpCommonStartPage) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonStartPage
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('common:gpCommonStartPage:export')")
    @Log(title = "导出启动页", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GpCommonStartPageReq gpCommonStartPageReq) {
        QueryWrapper<GpCommonStartPage> lqw = new QueryWrapper<GpCommonStartPage>();
        gpCommonStartPageReq.generatorQuery(lqw, true);
        List<GpCommonStartPageVo> list = iGpCommonStartPageService.listVo(lqw);
        ExcelUtil<GpCommonStartPageVo> util = new ExcelUtil<GpCommonStartPageVo>(GpCommonStartPageVo.class);
        return util.exportExcel(list, "gpCommonStartPage");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonStartPage
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('common:gpCommonStartPage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<GpCommonStartPageVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iGpCommonStartPageService.getVoById(id));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonStartPage
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('common:gpCommonStartPage:remove')")
    @Log(title = "删除启动页", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCommonStartPageService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
